package com.ewhale.feitengguest.presenter.shop.task

import com.ewhale.feitengguest.api.ApiHelper
import com.ewhale.feitengguest.dto.EditTaskDto
import com.ewhale.feitengguest.dto.ShopTaskDetailDto
import com.ewhale.feitengguest.dto.TaskCommentDto
import com.ewhale.feitengguest.dto.TaskDetailsDto
import com.ewhale.feitengguest.view.shop.task.STaskDetailView
import com.ewhale.feitengguest.view.shop.task.STaskView
import com.ewhale.feitengguest.view.user.task.TaskDetailsView
import com.simga.library.base.IPresenter
import com.simga.library.http.APIException

/**
 * 作者：SimGa
 * 日期：2020/2/27
 * 时间：10:27
 * 包名：com.ewhale.feitengguest.presenter.shop.task
 */
class STaskDetailPresenter : IPresenter() {
    fun loadTaskDetail(missionId: Long) {
        mView.showProLoading()
        request(1, ApiHelper.TASK_API.findMissionDetailByMissionId(missionId), null)
    }

    fun loadComment(missionId: Long, pageNum: Int) {
        request(2, ApiHelper.TASK_API.getTaskCommentByShop(missionId, pageNum, 10), pageNum)
    }

    fun loadTaskEditDetail(id: Long) {
        mView.showProLoading()
        request(3, ApiHelper.TASK_API.editMission(id), null)
    }

    fun deleteTask(id: Long) {
        mView.showProLoading()
        request(4, ApiHelper.TASK_API.deleteMission(id), null)
    }

    fun suspendTask(id: Long) {
        mView.showProLoading()
        request(5, ApiHelper.TASK_API.updateMissionStopStatus(id, 1), null)
    }

    fun startTask(id: Long) {
        mView.showProLoading()
        request(6, ApiHelper.TASK_API.updateMissionStopStatus(id, 0), null)
    }
    fun endTask(id: Long) {
        mView.showProLoading()
        request(7, ApiHelper.TASK_API.overMission(id), null)
    }
    fun repleyComment(id: Long, content: String, position: Int) {
        mView.showProLoading()
        val params = HashMap<String,Any>()
        params["position"] = position
        params["replyContent"] = content
        request(8, ApiHelper.TASK_API.replyContent(id,content), params)
    }

    override fun <T : Any?> onNext(postCode: Int, result: T, data: Any?) {
        super.onNext(postCode, result, data)
        when (postCode) {
            1 -> (mView as STaskDetailView).showTaskDetails(result as ShopTaskDetailDto)
            2 -> (mView as STaskDetailView).showTaskCommentList(result as List<TaskCommentDto>, data as Int)
            3 -> (mView as STaskDetailView).showEditDetail(result as EditTaskDto)
            4 -> (mView as STaskDetailView).deleteSuccess()
            5 -> (mView as STaskDetailView).suspendSuccess()
            6 -> (mView as STaskDetailView).startSuccess()
            7 -> (mView as STaskDetailView).endSuccess()
            8 -> (mView as STaskDetailView).replySuccess(data as HashMap<String,Any>)
        }
    }

    override fun otherException(postCode: Int, data: Any?, e: APIException?) {
        super.otherException(postCode, data, e)
        mView.showErrorMessage(postCode, e!!.code, e.displayMessage)
    }


}
